package com.have.blog.web.service.impl;

import com.have.blog.base.constant.ErrorCode;
import com.have.blog.base.exception.QueryException;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.WebConfigMapper;
import com.have.blog.model.models.WebConfig;
import com.have.blog.model.models.WebConfigExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.JsonUtils;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.web.dto.WebConfigDto;
import com.have.blog.web.service.WebConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@SuppressWarnings("all")
public class WebConfigServiceImpl implements WebConfigService {
    @Autowired
    private WebConfigMapper webConfigMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;

    @Override
    public WebConfigDto getWebConfig() {
        WebConfigExample example = new WebConfigExample();
        example.setOrderByClause("create_time desc");
        List<WebConfig> webConfigList = webConfigMapper.selectByExample(example);
        WebConfig webConfig = webConfigList.get(0);
        WebConfigDto result = new WebConfigDto();
        MyBeanUtils.copyProperties(webConfig, result);

        // TODO 上图标
        if (webConfig.getLogoUid() != null) {
            Wrapper<String> wrapper = pictureFeignApi.getPictureUrl(webConfig.getLogoUid());
            if (wrapper.isSuccess()) {
                result.setPhotoUrl(wrapper.getData());
            }
        }
        if (webConfig.getAliPayPictureUid() != null){
            Wrapper<String> wrapper = pictureFeignApi.getPictureUrl(webConfig.getLogoUid());
            if (wrapper.isSuccess()) {
                result.setAliPayPhotoUrl(wrapper.getData());
            }
        }
        if (webConfig.getWeixinPayPictureUid() != null){
            Wrapper<String> wrapper = pictureFeignApi.getPictureUrl(webConfig.getLogoUid());
            if (wrapper.isSuccess()) {
                result.setWeixinPayPhotoUrl(wrapper.getData());
            }
        }
        return result;
    }
    @Override
    public boolean isOpenLoginType(String loginType) {
        WebConfigDto webConfig = getWebConfig();
        if (webConfig == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_IS_NOT_EXIST, "SYSTEM_CONFIG_IS_NOT_EXIST");
        }
        String loginTypeList = webConfig.getLoginTypeList();
        List<String> loginTypes = JsonUtils.jsonToList(loginTypeList, String.class);
        return true;
    }
}
